package com.angel.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import com.angel.cv.pojo.Cv;
import com.angel.cv.pojo.CvBasic;
import com.angel.cv.pojo.CvEdu;
import com.angel.cv.pojo.CvExpectation;
import com.angel.cv.pojo.CvJob;
import com.angel.cv.pojo.CvPro;
import com.angel.cv.pojo.CvSkill;
import com.zph.entity.BasicNow;
import com.zph.entity.CVObject;
import com.zph.entity.EducationNow;
import com.zph.entity.Expectation;
import com.zph.entity.Job;
import com.zph.entity.Project;
import com.zph.entity.Skill;

/**
 * @Des     格式转换
 * @author	张振华
 * @date	2017年4月24日  下午6:02:26
 * @version v1.0
 */
public class Transformation {
	
	public static AngelObject  trans(CVObject map) {
		AngelObject ao = new AngelObject(); 
		
		//Cv信息
		transCv(map,ao);
		//基本信息
		transBasic(map.getBasic(),ao); 
		//教育信息
		transEdu(map.getEducations(),ao); 
		//期望信息
		transHope(map.getExpectation(),ao); 
		//工作信息
		transJob(map.getJobs(),ao); 
		//项目信息
		transPro(map.getProjects(),ao); 
		//技能信息
		transSkill(map.getSkills(),ao); 
		return ao;
		
	}

	 

	
	
	private static void transCv(CVObject map, AngelObject ao) {
		Cv cv = new Cv();
		cv.setOriginalFilename(getCvName(map.getFilePath()));
		cv.setCvInfo(map.getCvInfo()); 
		ao.setCv(cv); 
		
	} 

	private static void transBasic(BasicNow basic, AngelObject ao) {
		if(basic!=null){  
			CvBasic cvBasic = new CvBasic(); 
			cvBasic.setName(basic.getName());
			cvBasic.setGender(getSexNum(basic.getGender()));
			cvBasic.setIdcard(basic.getIdCard());
			cvBasic.setCompany(basic.getIdCard());
			cvBasic.setTitle(basic.getTitle());
			cvBasic.setPhone(basic.getPhone());
			cvBasic.setMobile(basic.getMobile());
			cvBasic.setEmail(basic.getEmail());
			cvBasic.setAge(basic.getAge());
			cvBasic.setBirthday(basic.getBirthday()); //待完善
			cvBasic.setMarriage(basic.getMarriage());
			cvBasic.setMajor(basic.getMajor());
			cvBasic.setDegree(basic.getDegree().getId()+"");
			cvBasic.setSchool(basic.getSchool());
			cvBasic.setHeight(basic.getHeight());
			cvBasic.setWeight(basic.getWeight());
			cvBasic.setBwh(basic.getBwh());
			cvBasic.setNationality(basic.getNationality());
			cvBasic.setCountry(basic.getCountry());
			cvBasic.setHealth(basic.getHealth());
			cvBasic.setPolitics(basic.getPolitics());
			cvBasic.setWeixin(basic.getWeixin());
			cvBasic.setQq(basic.getQq());
			cvBasic.setLinkedin(basic.getLinkedIn());
			cvBasic.setDajie(basic.getDajie());
			cvBasic.setWeibo(basic.getWeibo());
			cvBasic.setAddress(basic.getAddress());
			cvBasic.setTrade(basic.getTradeValue()); //待完善
//			cvBasic.setFunction(basic.getFunctionId());
//			cvBasic.setPhotoUrl(photoUrl);
			cvBasic.setPreference(basic.getPreference()); 
//			cvBasic.setCompanyId(basic.getCompany());
			if(basic.getJob_time()!=null)
			cvBasic.setJobTime((int)(long)basic.getJob_time());
//			cvBasic.setWorkYear(basic.get);
			
			 
			ao.setBasic(cvBasic); 
		}
		
	}



	private static String getStaNum(int jobStatus) {
		switch(jobStatus){
		case 13://长期观望
			return "0";
		case 64://不看机会
			return "0";
		case 65://正在求职
			return "1";
		case 66: //黑名单
			return "0";
			
		}
		
		return null;
	}





	private static Integer getSexNum(int gender) {
		switch(gender){
		case 0:
		 return 1;
		case 1:
			 return 0; 
		}
		return 0;
	}





	private static Integer getMarrnum(int marriage) {
		switch(marriage){
		case 0:
			return 4;
		case 1:
			return 1;
		case 2:
			return 0;
		case 3:
			return 1;
		case 4:
			return 2;
		case 5:
			return 3;
		case 6:
			return 4;
		}
		return null;
	}





	@Test
	public void test02(){
		String str = "d:/1231/dasda/dsa\\ss.txt";
		System.out.println(getCvName(str));
	}

	private static String getCvName(String path) {
		String name=null;
		if(path!=null){
			if(path.lastIndexOf("/")>path.lastIndexOf("\\")){
				  name = path.substring(path.lastIndexOf("/")+1,path.length()); 
			}else{
				 name = path.substring(path.lastIndexOf("\\")+1,path.length()); 
			}
		}
		
		
		return name;
	}





	private static void transEdu(List<EducationNow> educations, AngelObject ao) {
		 if(educations!=null){
			 List<CvEdu> edus = new ArrayList<CvEdu>();
			 for(EducationNow edu:educations){
				 CvEdu cvEdu = new CvEdu();
				 cvEdu.setStartTime(dateFormate(edu.getStartTime()));
				 cvEdu.setEndTime(dateFormate(edu.getEndTime())); 
				 cvEdu.setSchool(edu.getSchool());
				 cvEdu.setMajor(edu.getMajor());
				 cvEdu.setDegree(edu.getDegree().getId());  
				 //学校类型 985、211
				 edus.add(cvEdu);
			 }
			 ao.setEdus(edus); 
		 }
		
	}





	/**
	 * <p>弃</p>
	 * @param degree
	 * @return
	 */
	private static String getDegree(int degree) {
		 switch(degree){
		 case 3:  //专科及以下
		 		return "4";
		 case 4:  //本科
			 	return "3";
		 case 5:  //硕士
			 	return "2";
		 case 6:  //博士
			 	return "1";
		 case 7:  //博士后
			 	return "0";
		 	
		 }
		return "5"; //其他
	}





	private static void transHope(Expectation exp, AngelObject ao) {
		 if(exp!=null){
			 CvExpectation cvExp = new CvExpectation();
			 cvExp.setExpTrade(exp.getExpectationIndustryValue());
			 cvExp.setExpFunction(exp.getExpectationFunctionValue());
			 cvExp.setExpCity(exp.getExpectationCityValue());
			 if(exp.getExpectationSalaryValue()!=null&&!exp.getExpectationSalaryValue().equals("")){
				 dealWithExpSal(cvExp,exp.getExpectationSalaryValue()); 
			 }
			 cvExp.setCurSalary(exp.getPresentSalary()+"");
			 cvExp.setJobStatus(exp.getJobstatus()); //带存储原始文本
			 cvExp.setExpCompany(exp.getExpectationCompany());
			 cvExp.setExpTitle(exp.getExpectationTitle());
			 ao.setExp(cvExp); 
		 }
		
	}





	private static void dealWithExpSal(CvExpectation cvExp, String expSalary) {
		Pattern p =  Pattern.compile("\\d+");
		Matcher m = p.matcher(expSalary);
		int i=0;
		while(m.find()){
			if(0==i){
				cvExp.setExpTopsalary(m.group());
			}else if(1==i){
				String topSa = m.group();
				int topS = Integer.parseInt(topSa);
				if(topS>Integer.parseInt(cvExp.getExpLowsalary())){
					cvExp.setExpTopsalary(topSa); 
				} 
			}
			i++;
		}
				
		
	}





	private static Integer getSalary(Long es) { 
		if(es!=null){
			switch((int)(long)es){
			case 67:
				return 50000; 
			case 68:
				return 100000; 
			case 69:
				return 200000; 
			case 70:
				return 300000;
			case 71:
				return 500000;
			case 72:
				return 1000000;
			case 73:
				return 3000000;
			case 74:
				return 5000000;
			case 75:
				return 10000000;
			
			}
			
		}
		
		return null;
	}





	private static void transJob(List<Job> jobs, AngelObject ao) {
		if(jobs!=null){
			List<CvJob> cvJobs = new ArrayList<CvJob>(); 
			for(Job job:jobs){
				CvJob cvJob = new CvJob();
				cvJob.setStartTime(dateFormate(job.getStartTime()));
				cvJob.setEndTime(dateFormate(job.getEndTime()));	
				cvJob.setCompany(job.getCompany());
//				cvJob.setCompanysize();
//				cvJob.setCompanynature();
				
				cvJob.setDepartment(job.getDepartment());
				cvJob.setTitle(job.getTitle());
				cvJob.setReport(job.getReport());
				cvJob.setSalary(job.getSalary());
				cvJob.setMember(job.getMember());
				cvJob.setDuty(job.getDuty());
				cvJob.setAchieve(job.getAchieve());
				cvJob.setReason(job.getReason());
				cvJob.setCity(job.getCity());
				cvJob.setInfo(job.getInfo()); //其他信息
//				cvJob.setTrade(job.get);				
				cvJobs.add(cvJob);
			
				
			}
			ao.setJobs(cvJobs); 
		}
		
	}





	private static Integer getNum(String member) {
		 if(member!=null){
			Pattern p = Pattern.compile("[0-9]");
			Matcher m = p.matcher(member);
			Integer num = 0;
			while(m.find()){
				num = Integer.parseInt(m.group());
			}
			return num;
		 }
		return null;  
	}





	private static void transPro(List<Project> projects, AngelObject ao)  {
		 if(projects!=null){
			List<CvPro> cvPros = new ArrayList<CvPro>();  
			for(Project pro:projects){
				CvPro cvPro = new CvPro(); 
				cvPro.setStartTime(dateFormate(pro.getStartTime()));
				cvPro.setEndTime(dateFormate(pro.getEndTime())); 
				cvPro.setProjectName(pro.getName());
				cvPro.setCompany(pro.getCompany());
				cvPro.setContent(pro.getContent());
				cvPro.setDuty(pro.getDuty());
				cvPro.setAchieve(pro.getAchieve());
				cvPro.setCity(pro.getCity());
				cvPros.add(cvPro);
				
			}
			ao.setPros(cvPros); 
		 }
		
	}





	/**
	 *  日期格式转换
	 * @param time
	 * @return
	 */
	private static Date dateFormate(String time) {
		if(time!=null){
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
				Pattern p = Pattern.compile("([0-9]{4})[^0-9]+([0-9]{2})");
				Matcher m = p.matcher(time);
				if(m.find()){
					if(m.groupCount()==2){
						String year = m.group(1); 
						String month =m.group(2);
						return sdf.parse(year+"/"+month);
					} 
				}else if(time.indexOf("至今")!=-1){
					return new Date();
				}
			} catch (ParseException e) { 
				e.printStackTrace();
			} 
		}
		return null;
	}
	
	@Test
	public void test(){
		String str = "2015-03dada"; 
		System.out.println(dateFormate(str)); 
	}
	

	/**
	 * @param skills
	 */
	private static void transSkill(List<Skill> skills, AngelObject ao) {
		 if(skills!=null){
			 List<CvSkill> cvSkills  = new ArrayList<CvSkill>();
				for(Skill skill:skills){
					CvSkill cvSkill = new CvSkill();
					cvSkill.setSkillname(skill.getSkillName());
					cvSkill.setMonth(skill.getSkillExperience());
					cvSkill.setLevel(skill.getSkillInfo());
					cvSkills.add(cvSkill);
					 
				}
				ao.setSkills(cvSkills); 
		 }
		
		
	}
	
}
